<template>
  <div>
    <!-- 搜索栏 -->
    <el-input
      v-model="search"
      :placeholder="t('searchUser')"
      style="width: 200px; margin: 0 8px 16px 0"
      clearable
      @input="onSearch"
    />
    <el-button type="danger" size="small" :disabled="!multipleSelection.length" @click="batchDelete">
      {{ t('batchDelete') }}
    </el-button>
    <el-button type="primary" @click="openDialog" style="float:right">{{ t('addUser') }}</el-button>
    <el-table
      :data="pagedUsers"
      style="width: 100%; margin-top: 8px"
      @selection-change="handleSelectionChange"
      ref="userTable"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="username" :label="t('username')" />
      <el-table-column prop="role" :label="t('role')" />
      <el-table-column :label="t('actions')" width="160">
        <template #default="scope">
          <el-button size="mini" @click="editUser(scope.row)">{{ t('edit') }}</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row)">{{ t('delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 16px; text-align: right"
      background
      layout="prev, pager, next, jumper"
      :total="filteredUsers.length"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handlePageChange"
    />
    <el-dialog :visible.sync="dialogVisible" :title="t('userInfo')">
      <el-form :model="form" label-width="80px">
        <el-form-item :label="t('username')">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item :label="t('role')">
          <el-select v-model="form.role">
            <el-option :label="t('admin')" value="admin" />
            <el-option :label="t('user')" value="user" />
          </el-select>
        </el-form-item>
        <!-- 权限扩展点 -->
        <!-- <el-form-item label="权限">...</el-form-item> -->
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">{{ t('cancel') }}</el-button>
        <el-button type="primary" @click="saveUser">{{ t('save') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue'

const users = ref([
  { id: 1, username: 'admin', role: 'admin' },
  { id: 2, username: 'user1', role: 'user' }
])
const dialogVisible = ref(false)
const form = ref({ id: null, username: '', role: '' })
const search = ref('')
const pageSize = 5
const currentPage = ref(1)
const multipleSelection = ref([])

const filteredUsers = computed(() =>
  users.value.filter(u => u.username.includes(search.value))
)
const pagedUsers = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  return filteredUsers.value.slice(start, start + pageSize)
})

function onSearch() {
  currentPage.value = 1
}
function handlePageChange(page) {
  currentPage.value = page
}
function handleSelectionChange(val) {
  multipleSelection.value = val
}
function batchDelete() {
  users.value = users.value.filter(u => !multipleSelection.value.includes(u))
  multipleSelection.value = []
}
function openDialog() {
  form.value = { id: null, username: '', role: '' }
  dialogVisible.value = true
}
function editUser(row) {
  form.value = { ...row }
  dialogVisible.value = true
}
function saveUser() {
  if (!form.value.username || !form.value.role) return
  if (form.value.id) {
    const idx = users.value.findIndex(u => u.id === form.value.id)
    users.value[idx] = { ...form.value }
  } else {
    form.value.id = Date.now()
    users.value.push({ ...form.value })
  }
  dialogVisible.value = false
}
function deleteUser(row) {
  users.value = users.value.filter(u => u.id !== row.id)
}

const t = (key) => ({
  addUser: '新增用户',
  username: '用户名',
  role: '角色',
  actions: '操作',
  edit: '编辑',
  delete: '删除',
  userInfo: '用户信息',
  cancel: '取消',
  save: '保存',
  searchUser: '搜索用户名',
  batchDelete: '批量删除',
  admin: '管理员',
  user: '普通用户'
}[key] || key)
</script>